//
//  UgcMain_FirstFourPic_Cell_WX.m
//  ZITHER
//
//  Created by weixin on 2019/3/18.
//  Copyright © 2019 weixn. All rights reserved.
//

#import "UgcMain_FirstFourPic_Cell_WX.h"
#import "UgcMain_VC_WX.h"
#import "Ugc_HomePage_VC_V3.h"
#import "TopicDetailViewController.h"


@implementation UgcMain_FirstFourPic_Cell_WX


- (void)awakeFromNib {
    [super awakeFromNib];
    self.viewBorder.backgroundColor = [UIColor whiteColor];
    [self.viewBorder setCornerRadiusWithRadius:10 isMasks:YES];
    self.backgroundColor = HexColor(0xf5f5f5);

    [self.viewBorder mas_remakeConstraints:^(MASConstraintMaker *make) {
        make.left.top.equalTo(self).offset(15);
        make.right.equalTo(self).offset(-15);
        make.bottom.equalTo(self).offset(0);
    }];
    [self setUI];
    [self setMasonry];
    self.userInteractionEnabled = YES;
    
}

-(void)setModelSub:(CoverCreateListModelSub *)modelSub{
    _modelSub = modelSub;
    UgcMainListModel *model = [[UgcMainListModel alloc]init];
    model.avatar = modelSub.avatar;
    model.nickName = modelSub.nickName;
    model.createTime = modelSub.createTime;
    model.laudStatus = modelSub.laudStatus;
    model.laudCount = modelSub.laudCount;
    model.commentCount = modelSub.commentCount;
    model.ugcContent = modelSub.ugcContent;
    model.userId = modelSub.userId;
    model.fileList = modelSub.ugcFileList;
    model.ugcId = modelSub.relId;
    model.userSubjectTitle = modelSub.userSubjectTitle;
    
    self.model = model;
}

-(void)setModel:(UgcMainListModel *)model{
    
    _model = model;
    NSString *str = [NSString stringWithFormat:@"%@%@/zip",RootUrlImage,model.avatar];
    [self.userImage sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"ucg_img_loding"]];
    
    self.userName.text = model.nickName;
    //    self.like.selected = ![model.laudStatus isEqualToString:@"0"];
    [self.like setTitle:[NSString stringWithFormat:@" %@",model.laudCount] forState:UIControlStateNormal];
    [self changeLike:![model.laudStatus isEqualToString:@"0"]];
    self.comment.text = [NSString stringWithFormat:@"%@人已赞 · %@条评论",model.laudCount,self.model.commentCount];
//    [self.comment setTitle:[NSString stringWithFormat:@" %@",model.commentCount] forState:UIControlStateNormal];
    self.usercomment.text = model.ugcContent;
    
    //    if ([self.model.userId isEqualToString:kUSER_INFO_MANAGER.userId]) {
    //        self.userDelete.hidden = NO;
    //    }else{
    //        self.userDelete.hidden = YES;
    //    }
    for (int i = 0; i<4; i++) {
        NSString *strpic= [NSString stringWithFormat:@"%@%@/zip",RootUrlImage,model.fileList[i]];
        [self.arrImageView[i] sd_setImageWithURL:[NSURL URLWithString:strpic] placeholderImage:[UIImage imageNamed:@"ucg_img_loding"]];
    }
    
    if (model.userSubjectTitle.length>0 ) {
        self.TopicLab.text = [NSString stringWithFormat:@"    %@",model.userSubjectTitle];
        self.TopicIcon.hidden = NO;
        [self.TopicLab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.userImage.mas_bottom).offset(10);

        }];
    }else{
        self.TopicIcon.hidden = YES;
        self.TopicLab.text = @"";
        [self.TopicLab mas_updateConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(self.userImage.mas_bottom).offset(0);
        }];
    }
    
}


-(void)setMasonry{
    
    [self.userImage mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self).offset(30);
        make.left.equalTo(self).offset(30);
        make.width.height.mas_equalTo(30);
    }];
    
    [self.userName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(self.userImage.mas_centerY).offset(0);
        make.left.equalTo(self.userImage.mas_right).offset(10);
        make.height.mas_equalTo(15);
    }];
    

    [self.usercomment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.TopicLab.mas_bottom).offset(10);
        make.left.equalTo(self.userImage.mas_left).offset(0);
        make.right.equalTo(self.mas_right).offset(-30);

    }];
    
    
    //    self.userPic.backgroundColor = [UIColor redColor];
    CGFloat imgHeight = (kScreenW-60)/3.0-10;
    [self.userPic mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.usercomment.mas_bottom).offset(10);
        make.left.equalTo(self.userImage).offset(0);
        make.width.mas_equalTo(1%2*(imgHeight+10)+imgHeight);
        make.height.mas_equalTo(((kScreenW-60)/3.0)*2-10);
    }];

    
    
    [self.like mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.userPic.mas_bottom).offset(12);
        make.right.equalTo(self.mas_right).offset(-30);
        make.width.mas_equalTo(15);
        make.height.mas_equalTo(15);
    }];
    
    //    self.comment.backgroundColor = [UIColor randomColor];
    
    [self.comment mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.userPic.mas_bottom).offset(15);
        make.left.equalTo(self.userImage).offset(0);
        make.height.mas_equalTo(20);
        make.bottom.equalTo(self).offset(-10);
    }];
    
    [self.TopicLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.userImage.mas_bottom).offset(10);
        make.left.equalTo(self.userImage).offset(0);
        make.right.equalTo(self.mas_right).offset(-30);
    }];
    
    [self.TopicIcon mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.TopicLab.mas_top).offset(5);
        make.left.equalTo(self.TopicLab.mas_left).offset(0);
        make.size.mas_equalTo(CGSizeMake(10, 13));
    }];
}

-(void)setUI{
    [self addSubview:self.userImage];
    [self addSubview:self.userName];
    [self addSubview:self.like];
    [self addSubview:self.comment];
    [self addSubview:self.usercomment];
    [self addSubview:self.userPic];
    [self addSubview:self.TopicLab];
    [self addSubview:self.TopicIcon];
    for (UIImageView *imageView in self.arrImageView) {
        [self.userPic addSubview:imageView];
    }
}

-(UIImageView *)userImage{
    if (!_userImage) {
        _userImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
        [_userImage setCornerRadiusWithRadius:15 isMasks:YES];
        //        _userImage.contentMode = 2;
        _userImage.contentMode = UIViewContentModeScaleAspectFill;
        _userImage.contentScaleFactor = [[UIScreen mainScreen] scale];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
        [[tap rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
            Ugc_HomePage_VC_V3 *vc = [[Ugc_HomePage_VC_V3 alloc]init];
            vc.userName = self.model.nickName;
            vc.userId = self.model.userId;
            vc.userAvatar =  self.model.avatar;
            [self.getNav pushViewController:vc animated:YES];
        }];
        
        [_userImage addGestureRecognizer:tap];
        _userImage.userInteractionEnabled = YES;
    }
    return _userImage;
}

-(QMUILabel *)userName{
    if (!_userName) {
        _userName = [[QMUILabel alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
        _userName.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:13];
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
        [[tap rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
            Ugc_HomePage_VC_V3 *vc = [[Ugc_HomePage_VC_V3 alloc]init];
            vc.userName = self.model.nickName;
            vc.userId = self.model.userId;
            vc.userAvatar =  self.model.avatar;
            [self.getNav pushViewController:vc animated:YES];
        }];
        [_userName addGestureRecognizer:tap];
        _userName.userInteractionEnabled = YES;
    }
    return _userName;
}



-(BaseBtn *)like{
    if (!_like) {
        _like = [[BaseBtn alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
        [_like setImage:[UIImage imageNamed:@"Home_SegCtrl_tracking_active"] forState:UIControlStateNormal];
        [_like setImage:[UIImage imageNamed:@"Home_SegCtrl_tracking_active_p"] forState:UIControlStateSelected];
        //        [_like setTitleColor:baseColorTheme forState:UIControlStateSelected];
        //        [_like setTitleColor:[UIColor colorWithHex:@"999999"] forState:UIControlStateNormal];
        //        _like.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;// 水平左对齐
        //        _like.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:12];
        __weak __typeof(self)weakSelf = self;
        @weakify(self);
        [[_like rac_signalForControlEvents:(UIControlEventTouchUpInside)] subscribeNext:^(__kindof UIControl * _Nullable x) {
            @strongify(self);
            if (!weakSelf.like.selected) {
                [[serviceDefualt loadUpdateUserLaud:self.model.ugcId laudType:@"1" reponseObjctClass:nil]subscribeNext:^(ResponseData * x) {
                    if (x.success) {
                        [Tools showTips:@"点赞成功"];
                        self.model.laudStatus = @"1";
                        self.model.laudCount =  [NSString stringWithFormat:@" %ld",[self.model.laudCount integerValue]+1];
                        self.comment.text = [NSString stringWithFormat:@"%@人已赞 · %@条评论",self.model.laudCount,self.model.commentCount];

                        //                        [self.like setTitle:self.model.laudCount forState:UIControlStateNormal];
                        [self changeLike:YES];
                    }else{
                        if (![x.errorInfo isEqualToString:@"未登录"]) {
                            [Tools showTips:x.errorInfo];
                        }else{
                            //                            [Tools pushSinge];
                        }
                        self.model.laudStatus = @"0";
                        [self changeLike:NO];
                    }
                }];
            } else {
                @weakify(self);
                [[serviceDefualt loadUpdateUserLaud:self.model.ugcId laudType:@"2" reponseObjctClass:nil]subscribeNext:^(ResponseData * x) {
                    @strongify(self);
                    if (x.success) {
                        [Tools showTips:@"取消成功"];
                        self.model.laudStatus = @"0";
                        self.model.laudCount =  [NSString stringWithFormat:@" %d",[self.model.laudCount integerValue]-1];
                        self.comment.text = [NSString stringWithFormat:@"%@人已赞 · %@条评论",self.model.laudCount,self.model.commentCount];

                        //                        [self.like setTitle:self.model.laudCount forState:UIControlStateNormal];
                        [self changeLike:NO];
                    }else{
                        if (![x.errorInfo isEqualToString:@"未登录"]) {
                            [Tools showTips:x.errorInfo];
                        }else{
                            //                            [Tools pushSinge];
                        }
                        self.model.laudStatus = @"1";
                        [self changeLike:YES];
                    }
                }];
            }
            weakSelf.like.selected = !weakSelf.like.selected;
            
        }];
    }
    return _like;
}
-(void)changeLike:(BOOL)type{
    
    if (type) {
        self.like.selected = YES;
    }else{
        self.like.selected = NO;
    }
}

-(BaseLab *)comment{
    if (!_comment) {
        _comment = [BaseLab initCustomLab:@"0人已赞 · 0条评论" color:HexColor(0x999999) font:[UIFont fontWithName:@"PingFang-SC-Regular" size:12]];
        //        [_comment setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        //        [_comment setImage:[UIImage imageNamed:@"ucg_btn_comment"] forState:UIControlStateNormal];
        //        _comment.titleLabel.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:12];
        //        _comment.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Medium" size:12];
        //        [_comment setTitleColor:baseColorTheme forState:UIControlStateSelected];
        //        [_comment setTitleColor:[UIColor colorWithHex:@"999999"] forState:UIControlStateNormal];
        _comment.userInteractionEnabled = NO;
    }
    return _comment;
}



-(QMUILabel *)usercomment{
    if (!_usercomment) {
        _usercomment = [[QMUILabel alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
        _usercomment.numberOfLines = 0;
        //        _usercomment.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:13];
        _usercomment.font = [UIFont fontWithName:@"PingFang-SC-Regular" size:14];
        _usercomment.textColor = HexColor(0x333333);
    }
    return _usercomment;
}

-(UIView *)userPic{
    if (!_userPic) {
        _userPic = [[QMUILabel alloc]initWithFrame:CGRectMake(0, 0, 1, 1)];
        _userPic.userInteractionEnabled = YES;
        
    }
    return _userPic;
}

-(NSMutableArray *)arrImageView{
    if (!_arrImageView) {
        _arrImageView = [[NSMutableArray alloc]init];
        for (int i = 0; i<4; i++) {
            CGFloat imgHeight = (kScreenW-60)/3.0-10;
            UIImageView *imageV = [[UIImageView alloc]init];
            imageV.frame =  CGRectMake(i%2*(imgHeight+10), i/2*(imgHeight+10), imgHeight, imgHeight);
            imageV.tag = i+100;
            imageV.contentMode = 2;
            
            NSString *str = [NSString stringWithFormat:@"%@%@",RootUrlImage,self.model.fileList[i]];
            [imageV sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"ucg_img_loding"]];
            imageV.backgroundColor = [UIColor randomColor];
            [self.userPic addSubview:imageV];
            
            UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] init];
            
            [[tap rac_gestureSignal] subscribeNext:^(__kindof UIGestureRecognizer * _Nullable x) {
                if (self.showBigPic) {
                    self.showBigPic(self.indexPath,imageV.tag-100);
                }
            }];
            [imageV addGestureRecognizer:tap];
            imageV.userInteractionEnabled = YES;
            //            [imageV setCornerRadiusWithRadius:8 isMasks:YES];
            imageV.clipsToBounds = YES;
            [_arrImageView addObject:imageV];
        }
    }
    return _arrImageView;
}


-(BaseLab *)TopicLab{
    if (!_TopicLab) {
        _TopicLab = [BaseLab initCustomLab:@"     1232131" color:HexColor(0x333333) font:[UIFont fontWithName:@"PingFangSC-Semibold" size:15]];
        _TopicLab.numberOfLines = 2;
        _TopicLab.lineBreakMode = NSLineBreakByCharWrapping;
        [[_TopicLab addTag]subscribeNext:^(id  _Nullable x) {
            TopicDetailViewController * vc = [[TopicDetailViewController
                                               alloc]init];
            vc.topicId = self.model.userSubjectId;
            [self.getNav pushViewController:vc animated:YES];
        }];
    }
    return _TopicLab;
}

-(BaseImageV *)TopicIcon{
    if (!_TopicIcon) {
        _TopicIcon = [[BaseImageV alloc]init];
        
        _TopicIcon.image = [UIImage imageNamed:@"发布_#话题"];
        _TopicIcon.backgroundColor = [UIColor clearColor];
    }
    return _TopicIcon;
}

@end
